package com.hello.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hello.sky.constant.JwtClaimsConstant;
import com.hello.sky.context.BaseContext;
import com.hello.sky.mapper.DishMapper;
import com.hello.sky.mapper.SetmealMapper;
import com.hello.sky.mapper.ShoppingCartMapper;
import com.hello.sky.model.dto.ShoppingCartDTO;
import com.hello.sky.model.entity.Dish;
import com.hello.sky.model.entity.Setmeal;
import com.hello.sky.model.entity.ShoppingCart;
import com.hello.sky.service.ShoppingCartService;
import com.hello.sky.util.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author D
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addIntoCart(ShoppingCartDTO dto) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(userId);

        //该用户的该商品是否存在购物车
        ShoppingCart cart = baseMapper.selectListByCart(shoppingCart);
        if (cart != null) {
            //存在就加一
            cart.setNumber(cart.getNumber() + 1);
            baseMapper.updateItemNumber(cart);
            return;
        }

        //判断是菜品还是套餐
        if (dto.getDishId() != null){
            //菜品
            Dish dish = dishMapper.selectById(dto.getDishId());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
        }else {
            //套餐
            Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
        }

        //插入该用户的该菜品到购物车表
        shoppingCart.setNumber(1);
        baseMapper.addIntoCart(shoppingCart);
    }

    @Override
    public List<ShoppingCart> getCartByUserId(HttpServletRequest request) {
        String jwt = request.getHeader("authentication");
        Claims claims = JwtUtil.parseJwtThenReturnId(jwt);
        Long userId = Long.parseLong(claims.get(JwtClaimsConstant.USER_ID).toString());
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public void subItem(ShoppingCartDTO dto) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(dto, shoppingCart);

        ShoppingCart cart = baseMapper.selectListByCart(shoppingCart);
        if (cart.getNumber() == 1){
            //删除
            baseMapper.delItem(cart);
        }else {
            //减一
            cart.setNumber(cart.getNumber() - 1);
            baseMapper.subItem(cart);
        }

    }

    @Override
    public void cleanCart() {
        Long userId = BaseContext.getCurrentId();
        baseMapper.cleanCart(userId);
    }
}
